package com.neusoft.service.impl;

import com.neusoft.domain.*;
import com.neusoft.domain.vo.CartVo;
import com.neusoft.domain.vo.OrderDetailetVo;
import com.neusoft.domain.vo.OrdersVo;
import com.neusoft.mapper.BusinessDao;
import com.neusoft.mapper.FoodDao;
import com.neusoft.mapper.OrderDetailetDao;
import com.neusoft.mapper.OrdersDao;
import com.neusoft.service.CartService;
import com.neusoft.service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    OrdersDao ordersDao ;
    @Autowired
    CartService cartService ;
    @Autowired
    OrderDetailetDao orderDetailetDao ;
    @Autowired
    BusinessDao businessDao ;
    @Autowired
    FoodDao foodDao;

    @Override
    public Integer createOrders(Orders orders)  {
        // 订单的日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String orderDateStr = simpleDateFormat.format(new Date());
        orders.setOrderDate(orderDateStr);
        // 订单的状态，默认为0，表示未支付
        orders.setOrderState(0);
        // 保存订单的基本信息到数据库
        ordersDao.createOrders(orders);

        // 保存订单的详情
        Cart cart = new Cart();
        cart.setBusinessId(orders.getBusinessId());
        cart.setUserId(orders.getUserId());

        List<CartVo> cartList = cartService.listCart(cart);

        List<OrderDetailet> list = new ArrayList<>();
        for (Cart c : cartList) {
            OrderDetailet orderDetailet = new OrderDetailet();
            orderDetailet.setOrderId(orders.getOrderId());
            orderDetailet.setFoodId(c.getFoodId());
            orderDetailet.setQuantity(c.getQuantity());
            list.add(orderDetailet);

        }
        // 批量保存订单的详情
        orderDetailetDao.saveOrderDetailetBatch(list);

        // 订单详情保存完毕之后，清空购物车
        for (CartVo cartVo : cartList) {
            Cart c = new Cart();

            BeanUtils.copyProperties(cartVo,c);

            cartService.removeCart( c);
        }

        return orders.getOrderId();
    }

    @Override
    public OrdersVo getOrdersById(Integer orderId)  {
        // 查询该订单的基本信息
        Orders orders = ordersDao.getOrdersById(orderId);

        OrdersVo ordersVo = null;
        if (orders != null) {

            ordersVo = new OrdersVo();
            BeanUtils.copyProperties(orders,ordersVo);

            // 封装商家信息
            Business business = businessDao.getBusinessById(orders.getBusinessId());
            ordersVo.setBusiness(business);

            // 封装订单对应的菜品信息
            List<OrderDetailet> orderDetailets = orderDetailetDao.listOrderDetailetByOrderId(orderId);
            List<OrderDetailetVo> orderDetailetVos = new ArrayList<>();
            for (OrderDetailet orderDetailet : orderDetailets) {

                OrderDetailetVo orderDetailetVo = new OrderDetailetVo();
                BeanUtils.copyProperties(orderDetailet,orderDetailetVo);

                Integer foodId = orderDetailet.getFoodId();
                Food food = foodDao.getFoodById(foodId);
                orderDetailetVo.setFood(food);

                orderDetailetVos.add(orderDetailetVo);
            }

            // 再把集合添加到订单对象中
            ordersVo.setList(orderDetailetVos);

        }

        return ordersVo;
    }

    @Override
    public List<OrdersVo> listOrdersByUserId(String userId)  {

        List<Orders> ordersList = ordersDao.listOrdersByUserId(userId);

        List<OrdersVo> ordersVos = new ArrayList<>();
        // 遍历订单列表，进行封装数据
        for (Orders orders : ordersList) {

            OrdersVo ordersVo = new OrdersVo();
            BeanUtils.copyProperties(orders,ordersVo);
            // 封装商家信息
            Business business = businessDao.getBusinessById(orders.getBusinessId());
            ordersVo.setBusiness(business);

            List<OrderDetailet> orderDetailets = orderDetailetDao.listOrderDetailetByOrderId(orders.getOrderId());
            List<OrderDetailetVo> orderDetailetVos = new ArrayList<>();
            for (OrderDetailet orderDetailet : orderDetailets) {
                OrderDetailetVo orderDetailetVo = new OrderDetailetVo();
                BeanUtils.copyProperties(orderDetailet,orderDetailetVo);

                Integer foodId = orderDetailet.getFoodId();
                Food food = foodDao.getFoodById(foodId);
                orderDetailetVo.setFood(food);

                orderDetailetVos.add(orderDetailetVo);
            }

            ordersVo.setList(orderDetailetVos);

            ordersVos.add(ordersVo);
        }

        return ordersVos;
    }
}
